package com.dongdongshop.service.impl;

import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbOrderItem;
import com.dongdongshop.service.CartService;
import com.dongdongshop.service.TbItemService;
import com.dongdongshop.vo.CartVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@DubboService
public class CartServiceImpl implements CartService {

    @DubboReference
    private TbItemService itemService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     *
     * @param cookieCart
     * @param redisCart
     * @return
     */

    @Override
    public List<CartVo> redisCartAndCookieCart(List<CartVo> cookieCart, List<CartVo> redisCart) {
        for (CartVo cartVo : cookieCart) {
            for (TbOrderItem item : cartVo.getOrderItemList()) {
                redisCart = addCartList(redisCart, String.valueOf(item.getItemId()),item.getNum());
            }
        }
        return redisCart;
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public List<CartVo> getCartListInRedis(Long id) {
        List<CartVo> cartList = (List<CartVo>) redisTemplate.boundHashOps("cartList").get(id);
        return cartList;
    }

    @Override
    public void addCartListToRedis(List<CartVo> cartList, Long id) {
        redisTemplate.boundHashOps("cartList").put(id,cartList);
    }

    @Override
    public List<CartVo> addCartList(List<CartVo> cartList, String itemId, Integer num) {
        //1.查询商品信息
        TbItem item =  itemService.selectByItemId(itemId);
        //2.根据信息获取商家
        String sellerId = item.getSeller();
        //3.判断商品的商家是否在购物车
        CartVo cart = null;//创建一个对象
        for (CartVo cartVo : cartList) {
            if(Objects.equals(cartVo.getSellerName(),sellerId)){//判断是否有这个商家
                cart = cartVo;//如果该商家存在,就不需要继续判断,直接跳出
                break;
            }
        }
        //4.如果不存在,我们直接添加数据就行了
        if(cart == null){
            //new 一个vo类表示是一个商家
           cart = new CartVo();
           //商家的商品明细列表集合,TborderItem就是商品的明细
           List<TbOrderItem> items = new ArrayList<>();
           //商品明细对象
           TbOrderItem orderItem = new TbOrderItem();
           cart.setSellerName(sellerId);
           orderItem.setItemId(Long.valueOf(itemId));
           orderItem.setPrice(item.getPrice());
           orderItem.setTitle(item.getTitle());
           orderItem.setGoodsId(item.getGoodsId());
           orderItem.setNum(num);
           orderItem.setSellerId(item.getSellerId());
           orderItem.setTotalFee(new BigDecimal(num * item.getPrice().doubleValue()));
           orderItem.setPicPath(item.getImage());
           //放到商家的商品明细列表集合中
           items.add(orderItem);
           //将商品明细的集合set到商家对象
           cart.setOrderItemList(items);
           //将商家对象装入购物车列表
           cartList.add(cart);
        }else{
            //5.如果存在但是没有该商品,我们就追加一=数据在商家明细下
            //获取商家的商品明细
            List<TbOrderItem> items2 = cart.getOrderItemList();
            TbOrderItem order = ifitemVo(items2, itemId);
            if(order == null){
                //创建商品明细对象
                order = new TbOrderItem();
                order.setItemId(Long.valueOf(itemId));
                order.setPrice(item.getPrice());
                order.setTitle(item.getTitle());
                order.setGoodsId(item.getGoodsId());
                order.setNum(num);
                order.setTotalFee(new BigDecimal(num * item.getPrice().doubleValue()));
                order.setPicPath(item.getImage());
                order.setSellerId(item.getSellerId());
                //放到商家的商品明细列表集合中
                items2.add(order);
            }else{
                //6.如果商家和商品都存在那么就加数量
                order.setNum(order.getNum() + num);
                order.setTotalFee(new BigDecimal(order.getNum() * order.getPrice().doubleValue()));
                if(order.getNum() <= 0){
                    items2.remove(order);
                }
                if(items2.size() <= 0){
                    cartList.remove(cart);
                }
            }
        }
        return cartList;
    }

    private TbOrderItem ifitemVo( List<TbOrderItem> items,String itemId){
        for (TbOrderItem orderItem : items) {
            if(Objects.equals(orderItem.getItemId().toString(),itemId)){
               return orderItem;
            }
        }
        return null;
    }
}
